package com.lhz.framework.aspect;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import com.lhz.common.utils.XssUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

/**
 * @Author: LiHuaZhi
 * @Date: 2021/9/4App 22:34
 * @Description:
 **/
@Component
@Aspect
public class XssParamAspect {

    @Pointcut("execution(* com.lhz.project.system.controller..*.*(..))" +
            "&& !execution(* com.lhz.project.system.controller.base.FileUploadController.uploadFile(..))")
    public void xssPoint() {
    }

    @Before("xssPoint()")
    public void paramValid(JoinPoint point) {
        Object[] args = point.getArgs();
        for (Object o : args) {
            if (o instanceof Number || o instanceof MultipartFile) {
                continue;
            }

            if (o instanceof String) {
                XssUtils.stripXss(o.toString());
            } else {
                String paramStr = JSONUtil.toJsonStr(o);
                HashMap<String, Object> map = JSONUtil.toBean(paramStr, new TypeReference<>() {
                }, false);
                map.forEach((k, v) -> {
                    if (v instanceof String) {
                        XssUtils.stripXss(k, v.toString());
                    }
                });
                System.out.println(map);
            }
        }
    }
}
